<template>
  <a-menu
    mode="inline"
    :defaultSelectedKeys="[key]"
    :style="{  borderRight: 0 }"
    @click="titleClick"
    :defaultOpenKeys="[open]"
  >
    <a-menu-item key="机构首页">
      <i class="anticon">
        <svg class="icon svg-icon" aria-hidden="true">
          <use xlink:href="#iconFill" />
        </svg>
      </i>
      <span>机构首页</span>
    </a-menu-item>
    <a-sub-menu key="内部管理">
      <span slot="title">
        <i class="anticon">
          <svg class="icon svg-icon" aria-hidden="true">
            <use xlink:href="#iconzuzhijiagou" />
          </svg>
        </i>
        <span>内部管理</span>
      </span>
      <a-menu-item v-show="isshow('组织架构')" key="组织架构">组织架构</a-menu-item>
      <a-menu-item v-show="isshow('教室')" key="教室">教室</a-menu-item>
      <a-menu-item v-show="isshow('员工')" key="员工">员工</a-menu-item>
      <a-menu-item v-show="isshow('机构角色')" key="机构角色">机构角色</a-menu-item>
    </a-sub-menu>
    <a-sub-menu key="sub2">
      <span slot="title">
        <i class="anticon">
          <svg class="icon svg-icon" aria-hidden="true">
            <use xlink:href="#iconjiaowuguanli" />
          </svg>
        </i>
        <span>教务管理</span>
      </span>
      <a-menu-item v-show="isshow('课程')" key="课程">课程</a-menu-item>
      <a-menu-item v-show="isshow('班级')" key="班级">班级</a-menu-item>
      <a-menu-item v-show="isshow('学员')" key="学员">学员</a-menu-item>
      <a-menu-item v-show="isshow('排课')" key="排课">排课</a-menu-item>
      <a-menu-item key="题库组">题库组</a-menu-item>
      <a-menu-item v-show="isshow('上课时间设置')" key="上课时间设置">上课时间设置</a-menu-item>
      <a-menu-item v-show="isshow('节假日设置')" key="节假日设置">节假日设置</a-menu-item>
    </a-sub-menu>
    <a-sub-menu key="sub3">
      <span slot="title">
        <i class="anticon">
          <svg class="icon svg-icon" aria-hidden="true">
            <use xlink:href="#iconshangke" />
          </svg>
        </i>
        <span>课上课下</span>
      </span>
      <a-menu-item v-show="isshow('上课签到')" key="上课签到">上课签到</a-menu-item>
      <a-menu-item v-show="isshow('下课点评')" key="下课点评">下课点评</a-menu-item>
      <a-menu-item v-show="isshow('课后作业')" key="课后作业">课后作业</a-menu-item>
    </a-sub-menu>
    <a-sub-menu key="sub4">
      <span slot="title">
        <i class="anticon">
          <svg class="icon svg-icon" aria-hidden="true">
            <use xlink:href="#icongonggao" />
          </svg>
        </i>
        <span>公告推送</span>
      </span>
      <a-menu-item v-show="isshow('通知公告')" key="通知公告">通知公告</a-menu-item>
    </a-sub-menu>
    <a-sub-menu key="sub5">
      <span slot="title">
        <i class="anticon">
          <svg class="icon svg-icon" aria-hidden="true">
            <use xlink:href="#iconjingyinggaikuang-" />
          </svg>
        </i>
        <span>统计分析</span>
      </span>
      <a-menu-item v-show="isshow('经营概况')" key="经营概况">经营概况</a-menu-item>
      <a-menu-item v-show="isshow('教务报表')" key="教务报表">教务报表</a-menu-item>
    </a-sub-menu>
  </a-menu>
</template>

<script>
import { mapGetters } from "vuex";
import { filter, indexOf, uniq } from "underscore";
export default {
  name: "schoolleft",
  computed: {
    ...mapGetters(["getschoolauthtype", "getschoolrole", "getuser"]),
    userrole() {
      const { type, schoolroleid } = this.getuser;
      if (type === "root") {
        return this.getschoolauthtype;
      } else {
        if (schoolroleid) {
          const { authtypes } = this.getschoolrole.find(
            v => v.ID == schoolroleid
          );
          let arr = [];
          if (authtypes && authtypes.name) {
            arr = authtypes.name;
          }
          const data = filter(
            this.getschoolauthtype,
            v => indexOf(arr, v.ID.toString()) != -1
          );
          return data;
        } else {
          return [];
        }
      }
    },
    authtype() {
      function a(x, id) {
        let arr = [];
        for (let v in x) {
          if (x[v].pid == id) {
            x[v].children = uniq(a(x, x[v].ID));
            arr.push(x[v]);
          }
        }
        return arr;
      }
      const data = JSON.parse(JSON.stringify(this.getschoolauthtype));
      return a(data);
    }
  },
  // watch: {
  //   $route(v) {}
  // },
  beforeMount() {
    const name = this.$route.name;
    this.key = name;
    for (let v of this.authtype) {
      for (let vo of v.children) {
        if (vo.name == name) {
          this.open = v.name;
        }
      }
    }
    this.$router.beforeEach(this.beforeEach);
  },
  data() {
    return {
      key: "",
      open: ""
    };
  },
  props: ["collapsed"],
  methods: {
    titleClick(v) {
      this.key = v.key;
      const time = new Date().toLocaleDateString();
      if (this.key === "下课点评") {
        if (this.$route.name != v.key) {
          this.$router.push({
            name: v.key,
            params: {
              time
            }
          });
        }
      } else {
        if (this.$route.name != v.key) {
          this.$router.push({
            name: v.key
          });
        }
      }
    },
    beforeEach(v, _from, next) {
      let z = true;
      if (v.name) {
        let id = this.getschoolauthtype.find(vo => vo.name == v.name);
        if (id) {
          const x = this.userrole.find(vo => vo.name == id.name);

          if (!x) {
            this.$message.error("没有权限");
            z = false;
          }
        }
      }
      next(z);
    },
    isshow(name) {
      let z = true;
      let id = this.getschoolauthtype.find(vo => vo.name == name);
      if (id) {
        const x = this.userrole.find(vo => vo.name == id.name);
        if (!x) {
          z = false;
        }
      }
      return z;
    }
  }
};
</script>